;(function($, window, undefined) {
    
    var win = $(window),
        doc = $(document),
        count = 1,
        isLock = false;

    var Dialog = function(options) {
        
        this.settings = $.extend({}, Dialog.defaults, options);
        
        this.init();
        
    };
    
    Dialog.prototype = {
        
        /**
         * ��ʼ��
         */
        init : function() {
            
            this.create();
        
            if (this.settings.lock) {
                this.lock();
            }

            if (!isNaN(this.settings.time)&&this.settings.time!=null) {
                this.time();
            }
            
        },
        
        /**
         * ����
         */
        create : function() {

            var divHeader = (this.settings.title==null)?'':'<div class="rDialog-header-'+ this.settings.title +'"></div>';
            
            // HTMLģ��
            var templates = '<div class="rDialog-wrap">' +
                                divHeader +
                                '<div class="rDialog-content">'+ this.settings.content +'</div>' +
                                '<div class="rDialog-footer"></div>' +
                            '</div>';
            
            // ׷�ص�body
            this.dialog = $('<div>').addClass('rDialog').css({ zIndex : this.settings.zIndex + (count++) }).html(templates).prependTo('body');
            
            // ����ok��ť
            if ($.isFunction(this.settings.ok)) {
                this.ok();
            }
            
            // ����cancel��ť
            if ($.isFunction(this.settings.cancel)) {
               this.cancel(); 
            }
            
            // ���ô�С
            this.size();
            
            // ����λ��
            this.position();
            
        },
        
        /**
         * ok
         */
        ok : function() {
            var _this = this,
                footer = this.dialog.find('.rDialog-footer');
            
            $('<a>', {
                href : 'javascript:;',
                text : this.settings.okText
            }).on("click", function() {
                    var okCallback = _this.settings.ok();
                    if (okCallback == undefined || okCallback) {
                        _this.close();
                    }

            }).addClass('rDialog-ok').prependTo(footer);
            
        },
        
        /**
         * cancel
         */
        cancel : function() {
            
            var _this = this,
                footer = this.dialog.find('.rDialog-footer');
            
            $('<a>', {
                href : 'javascript:;',
                text : this.settings.cancelText
            }).on("click",function() {
                    var cancelCallback = _this.settings.cancel();
                    if (cancelCallback == undefined || cancelCallback) {
                        _this.close();
                    }
            }).addClass('rDialog-cancel').appendTo(footer);
            
        },
        
        /**
         * ���ô�С
         */
        size : function() {
            
            var content = this.dialog.find('.rDialog-content'),
            	wrap = this.dialog.find('.rDialog-wrap');
            
            content.css({ 
                width : this.settings.width,
                height : this.settings.height
            });
            //wrap.width(content.width());
        },
        
        /**
         * ����λ��
         */
        position : function() {
            
            var _this = this,
                winWidth = win.width(),
                winHeight = win.height(),
                scrollTop = 0;
            
            this.dialog.css({ 
                left : (winWidth - _this.dialog.width()) / 2,
                top : (winHeight - _this.dialog.height()) / 2 + scrollTop
            });

        },
        
        /**
         * ��������
         */
        lock : function() {
            
            if(!this.settings.nested){
			   if (isLock){return;} 
			}
			
            
            this.lock = $('<div>').css({ zIndex : this.settings.zIndex }).addClass('rDialog-mask');
            this.lock.appendTo('body');
            
            isLock = true;

        },
        
        /**
         * �ر�����
         */
        unLock : function() {
    		if (this.settings.lock) {
				if(this.settings.nested){
				   this.lock.remove();
				   isLock = false;
				}else{
					if (isLock) {
						this.lock.remove();
						isLock = false;
					}
				}
            }
        },
        
        /**
         * �رշ���
         */
        close : function() {
            this.dialog.remove();
            this.unLock();
        },
        
        /**
         * ��ʱ�ر�
         */
        time : function() {
            
            var _this = this;
            
            this.closeTimer = setTimeout(function() {
                _this.close();
            }, this.settings.time);

        }
        
    };
    
    /**
     * Ĭ������
     */
    Dialog.defaults = {
        
        // ����
        content: '������...',
        
        // ����
        title: 'load',
        
        // ���
        width: 'auto',
        
        // �߶�
        height: 'auto',
        
        // ȷ����ť�ص�����
        ok: null,
        
        // ȡ����ť�ص�����
        cancel: null,
        
        // ȷ����ť����
        okText: 'ȷ��',
        
        // ȡ����ť����
        cancelText: 'ȡ��',
        
        // �Զ��ر�ʱ��(����)
        time: null,
        
        // �Ƿ�����
        lock: true,

        // z-indexֵ
        zIndex: 9999,
		
		//������������õ���������
		nested: false
        
    };
    
    var rDialog = function(options) {
        return new Dialog(options);
    };
    
    window.rDialog = $.rDialog = $.dialog = rDialog;
    
})(window.jQuery || window.Zepto, window);
